<?php

/**
 * Project: Vietsam
 * Package Controller
 * Class: Tab
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *
 * */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Province extends MY_Controller {

    function __construct() {

        parent::__construct();

        $this->checkLogin();
        $this->checkPermission();

        $this->load->model('country_model', 'country', true);
        $this->load->model('province_model', 'province', true);
        $this->load->helper('form');
        $this->template->set_template('admin');
        $this->buildMenu();
    }

    function index() {
        $this->view();
    }

    function create() {
        $data = array();
        $data['id'] = 0;
        $data['id_country'] = '';
        $data['vi_name'] = '';
        $data['en_name'] = '';
        $data['abbreviation'] = '';
        $data['checked'] = 'checked';
        $data['position'] = $this->province->getMaxPosition() + 1;
        $data['error_message'] = '';
        $data['legend_text'] = lang('create_province');

        $countries = $this->country->getAll();
        $data_countries = array();
        for ($i = 0; $i < count($countries); $i++) {
            $data_countries[$countries[$i]['id']] = $countries[$i]['vi_name'];
        }

        $data['ddlCountries'] = form_dropdown('ddlCountries', $data_countries, 1);

        $this->template->parse_view('content', 'admin/province/create_province', $data);
        $this->template->render();
    }

    function edit($id) {
        $data = $this->province->getById($id);
        if (!$data) {
            redirect('admin/country');
            return;
        }

        $countries = $this->country->getAll();
        $data_countries = array();
        for ($i = 0; $i < count($countries); $i++) {
            $data_countries[$countries[$i]['id']] = $countries[$i]['vi_name'];
        }

        $data['ddlCountries'] = form_dropdown('ddlCountries', $data_countries, 1);
        
        $data['checked'] = $data['active'] == 1 ? 'checked' : '';
        $data['error_message'] = '';
        $data['legend_text'] = lang('edit_source');

        $this->template->parse_view('content', 'admin/province/create_province', $data);
        $this->template->render();
        //$this->main =  $this->load->view('admin/source/create_source', $data, true);
        //$this->execute();
    }

    function save() {
        $id = (int) $this->input->post('txtID');
        $source = new stdClass();
        $data = array();
        if ($id > 0) {
            $province = $this->province->getById($id);
            if (!$province) {
                redirect('admin/province');
                return;
            }
            $data = $province;
        } else {
            $data['id'] = 0;
        }

        $data['id'] = (int) $this->input->post('txtID');
        $data['id_country'] = trim($this->input->post('ddlCountries'));
        $data['abbreviation'] = trim($this->input->post('txtAbbreviation'));
        $data['numcode'] = trim($this->input->post('txtNumcode'));
        $data['vi_name'] = trim($this->input->post('txtViName'));
        $data['en_name'] = trim($this->input->post('txtEnName'));
        $data['checked'] = trim($this->input->post('ckActive')) == 'on' ? 'checked' : '';
        $data['active'] = trim($this->input->post('ckActive')) == 'on' ? 1 : 0;
        $data['position'] = intval($this->input->post('txtPosition'));
        $error = false;
        $error_message = '';

        $this->load->library('form_validation');
        $this->form_validation->set_rules('txtViName', 'lang:vi_name', 'required|min_length[3]|max_length[50]');
        $this->form_validation->set_rules('txtEnName', 'lang:en_name', 'required|min_length[3]|max_length[50]');
        $this->form_validation->set_rules('txtAbbreviation', 'lang:Abbreviation', 'required|min_length[2]|max_length[50]');

        if ($this->form_validation->run() == FALSE) {
            $data['error_message'] = '<div style="color:red">' . validation_errors() . '</div>';
            $data['legend_text'] = $id > 0 ? lang('edit_source') : lang('create_source');

            $countries = $this->country->getAll();
            $data_countries = array();
            for ($i = 0; $i < count($countries); $i++) {
                $data_countries[$countries[$i]['id']] = $countries[$i]['vi_name'];
            }
            $data['ddlCountries'] = form_dropdown('ddlCountries', $data_countries, $data['id_country']);

            $this->template->parse_view('content', 'admin/province/create_province', $data);
            $this->template->render();
        } else {
            if ($id > 0) {
                $result = $this->province->update($data);
            } else {
                $id_province = $this->province->insert($data);
            }

            redirect('admin/province/');
        }
    }

    function view($id_country = 0, $page = 0) {
        $NUMBER_RECORD_PER_PAGE = 60;
        $this->load->library('pagination');
        $page = (int)$page;// $this->uri->segment(4);
        $config['base_url'] = $this->base_url . 'admin/province/view/' . $id_country;
        $config['total_rows'] = $this->province->getAllCount($id_country);
        $config['per_page'] = $NUMBER_RECORD_PER_PAGE;
        $config['uri_segment'] = 5;
        $config['first_link'] = 'First';

        $this->pagination->initialize($config);

        $rows = $this->province->getAll($page, $NUMBER_RECORD_PER_PAGE, $id_country);
        $data = array();
        $data['rows'] = $rows;

        $countries = $this->country->getAll();
         $data_countries[0] = 'Quốc gia';
        for ($i = 0; $i < count($countries); $i++) {
            $data_countries[$countries[$i]['id']] = $countries[$i]['vi_name'];
        }
        $data['ddlCountries'] = form_dropdown('ddlCountries', $data_countries, $id_country, 'id = "ddlCountries" onchange = "changeCountry();"');
        
        $this->template->parse_view('content', 'admin/province/province_list', $data);
        $this->template->render();
    }

    function delete() {
        $id = intval($this->uri->segment(4));
        $ids = array();
        if ($id > 0) {
            $ids[] = (int) $id;
            //$this->product->delete($id);
        } else {
            if (($this->input->post('submitDelcategory'))) {
                $ids = $this->input->post('categoryBox');
            }
        }

        if (count($ids)) {

            $result = $this->country->delete($ids);
        }

        redirect('admin/country/');
    }

    
    }

?>
